package com.flute.framework.autoprompt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.flute.tools.data.mysql.ConnectionInformation;
import com.flute.tools.util.FileUtil;

public class SongData {
	public static void main(String[] args) throws IOException {
		ConnectionInformation info = new ConnectionInformation();
		info.setDbName("yyjd");
		info.setHost("localhost");
		info.setPassword("");
		info.setUserName("root");
		info.setPort("3306");
		// MysqlHandler handler = new MysqlHandler(info);

		Hashtable<String, Integer> map = new Hashtable<String, Integer>();

		// Iterator<DataRecord> it = handler
		// .iterator("select song_name,singer_names,album_names from
		// quannei_song_indexdata");
		// int i = 0;
		// while (it.hasNext()) {
		// DataRecord dr = it.next();
		// countText(map, dr.getFieldValue("song_name"));
		// countText(map, dr.getFieldValue("singer_names"));
		// countText(map, dr.getFieldValue("album_names"));
		//
		// System.out.println("sql:" + i++);
		// }
		//
		// FileUtil.output(map, -1, "record.txt");

		String[] lines = FileUtil.getContentByLine("r.txt", "utf8");
		int i = 0;
		// HashMap<String, String> splitedMap = new HashMap<String, String>();
		for (String line : lines) {
			int index = line.lastIndexOf(",");
			String s = line.substring(0, index);
			map.put(s, Integer.valueOf(line.substring(index + 1)));
			System.out.println(i++);
			// for (int j = 1; j < s.length(); j++) {
			// splitedMap.put(s.substring(0, j), s);
			// }
			// System.out.println("size:"+splitedMap.size());
		}

		AutoPrompt ap = AutoPrompt.getInstance();
		i = 0;
		Iterator<Entry<String, Integer>> entryIterator = map.entrySet()  
		                .iterator();  
		while (entryIterator.hasNext()) {
			Entry<String, Integer> entry = entryIterator.next();
			System.out.println("train:" + i++);
			List<TrainningToken> tl = new CommonPriorityStrategy()
					.getWordResult(entry.getKey());
			for (TrainningToken token : tl) {
				token.getToken().updatePriority(entry.getValue(), 2);
				// ap.train(token);
			}
			if (i > 1000000) {
				break;
			}
		}

		InputStreamReader stdin = new InputStreamReader(System.in);// 键盘输入
		BufferedReader bufin = new BufferedReader(stdin);
		while (Boolean.TRUE) {
			System.out.print("请输入字符:");
			String str = bufin.readLine();
			long start = System.currentTimeMillis();
			System.out.println(ap.getPromptValues(str));
			System.out.println("time:" + (System.currentTimeMillis() - start));
		}
	}

	// private static void countText(Hashtable<String, Integer> map, String
	// text) {
	// text = text.trim();
	// if (!map.containsKey(text)) {
	// map.put(text, 0);
	// }
	// map.put(text, map.get(text) + 1);
	// }
}
